package com.hengpeng.itfinoss.controller.stm;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hengpeng.common.acl.vo.Passport;
import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.entity.AbstractEntity.BoolValue;
import com.hengpeng.common.persist.query.model.Order.OrderType;
import com.hengpeng.common.web.tags.ListTag.Pagination;
import com.hengpeng.itfinbase.persist.Comment;
import com.hengpeng.itfinbase.persist.Comment.CommentType;
import com.hengpeng.itfinoss.controller.BaseController;
import com.hengpeng.itfinoss.controller.vo.CommentVo;
import com.hengpeng.itfinoss.service.ServiceFactory;
import com.hengpeng.itfinoss.util.BeanClone;

/**
 * <pre>
 * TODO 留言信息控制器
 * </pre>
 * @author pengfangliang
 * @version 1.0, 2010-9-15
 */
@Controller
@RequestMapping("/cmtm")
public class CommentController extends BaseController
{
	@Autowired
	private ServiceFactory serviceFactory;

	@RequestMapping(value = "/list")
	public String list(ModelMap map, HttpServletRequest request, CommentType commentType,
			BoolValue auditState, BoolValue replyState, String content, String reply, String title,
			String commUserName, String replyUserName, BoolValue isHidden)
	{
		map.put("commentType", commentType);
		map.put("auditState", auditState);
		map.put("replyState", replyState);
		map.put("content", content);
		map.put("reply", reply);
		map.put("title", title);
		map.put("commUserName", commUserName);
		map.put("replyUserName", replyUserName);
		map.put("isHidden", isHidden);
		Pagination page = new Pagination(request);
		page.setPageSize(this.getPageSize(request));
		LinkedHashMap<String, OrderType> sortMap = new LinkedHashMap<String, OrderType>();
		sortMap.put("replyState", OrderType.ASC);
		sortMap.put("postTime", OrderType.DESC);
		try
		{

			isHidden = isHidden == null ? BoolValue.NO : isHidden;
			QueryResult<Comment> result = serviceFactory.getCommentService().selectCommentWithPage(
					commentType, auditState, replyState, content, reply, title, commUserName, replyUserName, isHidden,
					page.currentPage, page.pageSize, sortMap);
			List<Comment> comments = result.getResults();
			List<CommentVo> commentVoList = new ArrayList<CommentVo>();
			commentVoList = BeanClone.clone(comments, CommentVo.class);
			if (commentVoList != null && commentVoList.size() != 0)
			{
			}
			page.setEntityCount((int) result.getTotalCount());
			map.put("commentlist", commentVoList);
			map.put("commentListPage", page);
			return "/stm/comment/comment_list";
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}

	@RequestMapping(value = "/new")
	public String perAdd(ModelMap map)
	{
		map.put("comment", new Comment());
		return "/stm/comment/detail";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String add(ModelMap map, HttpServletRequest request, String content, String title)
	{
		if (content.length() > 1000)
		{
			return returnError(map, "内容不能超过1000个字符。");
		}
		Passport passport = getUserSession(request.getSession());
		Comment comment = new Comment();
		comment.setAuditState(BoolValue.NO);
		comment.setReplyState(BoolValue.NO);
		comment.setIsHidden(BoolValue.NO);
		comment.setContent(content);
		comment.setTitle(title);
		if (passport.getUserId() == null)
			comment.setCommUserName("匿名用户");
		else
			comment.setCommUserName(passport.getUserId());
		try
		{
			ServiceResult<Comment> result = serviceFactory.getCommentService().save(comment);
			return returnSuccess(map, result, "/cmtm/list");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}

	@RequestMapping(value = "/get/{id}")
	public String get(@PathVariable Long id, ModelMap map)
	{
		if (id == null)
			return returnError(map, "留言ID不能为空！");
		Comment comment = serviceFactory.getCommentService().get(id).getReturnValue();
		if (comment == null)
			return returnError(map, "此留言不存在！");
		CommentVo commentVo = BeanClone.clone(comment, CommentVo.class);
		map.put("comment", commentVo);
		return "/stm/comment/detail";
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.POST)
	public String modify(ModelMap map, HttpServletRequest request, @PathVariable Long id, String reply,
			BoolValue isHidden, BoolValue auditState)
	{

		if (reply.length() > 1000)
		{
			return returnError(map, "回复内容不能超过1000个字符。");
		}

		Passport passport = getUserSession(request.getSession());
		Comment comment = serviceFactory.getCommentService().get(id).getReturnValue();
		comment.setReplyState(BoolValue.YES);
		comment.setReply(reply);
		comment.setReplyUserName(passport.getUserId());
		comment.setAuditState(auditState);
		comment.setIsHidden(isHidden);
		try
		{
			ServiceResult<Comment> result = serviceFactory.getCommentService().update(comment);
			return returnSuccess(map, result, "/cmtm/list");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}

	@RequestMapping(value = "/check/{id}")
	@ResponseBody
	public String check(@PathVariable Long id)
	{
		String flag = null;
		try
		{
			Comment comment = serviceFactory.getCommentService().get(id).getReturnValue();
			comment.setAuditState(BoolValue.YES);
			serviceFactory.getCommentService().update(comment);
			flag = "YES";
		}
		catch (Exception e)
		{
			flag = "NO";
		}

		return flag;
	}

	@RequestMapping(value = "/hidden/{id}")
	@ResponseBody
	public String hidden(@PathVariable Long id)
	{
		String flag = null;
		try
		{
			Comment comment = serviceFactory.getCommentService().get(id).getReturnValue();
			comment.setIsHidden(BoolValue.YES);
			serviceFactory.getCommentService().update(comment);
			flag = "YES";
		}
		catch (Exception e)
		{
			flag = "NO";
		}

		return flag;
	}

	@RequestMapping(value = "/remove")
	public String delete(ModelMap map, HttpServletRequest request, Long[] ids)
	{
		if (ids == null || ids.length == 0)
			return returnError(map, "删除的留言ID不能为空！");
		try
		{
			Set<Object> set = new HashSet<Object>(Arrays.asList(ids));
			ServiceResult<Comment> result = serviceFactory.getCommentService().delect(set);
			return returnSuccess(request, map, result, "/cmtm/list");
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return returnError(map, e);
		}
	}
}
